import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_base_library/http/http.dart';
import 'package:flutter_base_library/log/log.dart';
import 'package:flutter_base_library/utils/ReponseConfig.dart';
import 'package:flutter_base_library/utils/modal_progress_hud.dart';
import 'package:flutter_base_library/widget/loading_dialog.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_base_library/utils/json_util.dart';

abstract class BasePage extends StatefulWidget {
  //延时默认100毫秒调用initPageData()
  int delayedInitPageDataFunc = 100;

  @override
  CommonBasePageState createState() => createBaseState();

  CommonBasePageState createBaseState();
}

abstract class CommonBasePageState<T extends BasePage> extends State<BasePage> {
  bool inAsyncCall = false;
  double opacity = 0.5;
  Color screenLoadingColor = Colors.white;
  String loadingContent = "";
  TextStyle textStyle;

  @override
  void initState() {
    super.initState();

    initBaseParams();
    Future.delayed(Duration(milliseconds: widget.delayedInitPageDataFunc), () {
      initPageData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return ModalProgressHUD(
      inAsyncCall: inAsyncCall,
      opacity: opacity,
      loadingContent: loadingContent,
      textStyle: textStyle,
      // progressIndicator: SpinKitPouringHourglass(color: Colors.white),
      progressIndicator: SpinKitCircle(color: screenLoadingColor),
      child: buildPageWidget(context),
    );
    // return buildPageWidget(context);
  }

  Widget buildPageWidget(BuildContext context);

  //初始化页面基本参数
  void initBaseParams();

  //初始化页面数据
  void initPageData();

  //页面中展示加载框
  void request2Post(
      String url, BuildContext context, Map<String, dynamic> param,
      {bool isShowLoading = true,
      String loadingContent = "请求中",
      bool isRequestJson = true, // 请求是否是Json
      Function onSuccess,
      int connectTimeOut = 0,
      Function(int code, String message) onFail}) {
    if (isShowLoading) {
      setState(() {
        inAsyncCall = true;
        loadingContent = loadingContent;
      });
    }
    if (isRequestJson) {
      Http.postJson(url, param, connectTimeOut = connectTimeOut)
          .then((response) {
        setState(() {
          inAsyncCall = false;
        });
        disposeResponse(response, onSuccess: onSuccess, onFail: onFail);
      });
    } else {
      Http.post(
        url,
        param,
      ).then((response) {
        setState(() {
          inAsyncCall = false;
        });
        disposeResponse(response, onSuccess: onSuccess, onFail: onFail);
      });
    }
  }

  //以dialogLoading展示加载框
  void post2Server(String url, BuildContext context, Map<String, dynamic> param,
      {bool isShowLoading = true, //请求过程中是否要展示loading
      bool isRequestJson = true, // 请求是否是Json
      Color loadingIconColor = Colors.white,
      Color loadBackColor = Colors.red,
      String loadingContent = "请求中",
      bool showContent = false,
      Function onSuccess,
      Function(int code, String message) onFail}) {
    if (isShowLoading) {
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return LoadingDialog(
              // key: _formKey,
              showContent: showContent,
              backgroundColor: loadBackColor,
              content: loadingContent,
              loadingColor: loadingIconColor,
              loadingView: SpinKitCircle(color: loadingIconColor),
            );
          });
    }
    if (isRequestJson) {
      Http.postJson(
        url,
        param,
      ).then((response) {
        if (isShowLoading) {
          Navigator.pop(context); //直接传入context关闭就行
        }
        disposeResponse(response, onSuccess: onSuccess, onFail: onFail);
      });
    } else {
      Http.post(
        url,
        param,
      ).then((response) {
        if (isShowLoading) {
          Navigator.pop(context); //直接传入context关闭就行
        }
        disposeResponse(response, onSuccess: onSuccess, onFail: onFail);
      });
    }
  }

  void disposeResponse(data,
      {Function onSuccess, Function(int code, String message) onFail}) {
    Log.D("=====post请求的响应式是======");
    Map responseMap;
    JsonUtil.getObject(data, (v) => responseMap = v);
    Log.D("responseMap : " + responseMap.toString());
    Log.D("=====post请求的响应式是=====code=");
    ReponseConfig co = ReponseConfigUtils.getResponseConfig();

    int code = responseMap[co.responseCodeName];
    if (code == Http.httpDioConfig.successCode) {
      onSuccess(responseMap);
    } else {
      onFail(code, responseMap[co.responseMessageName]);
    }
  }
}
